package com.bargetor.cubilose

import cn.jpush.api.push.model.Message
import cn.jpush.api.push.model.Options
import cn.jpush.api.push.model.Platform
import cn.jpush.api.push.model.PushPayload
import cn.jpush.api.push.model.audience.Audience
import cn.jpush.api.push.model.notification.IosAlert
import cn.jpush.api.push.model.notification.IosNotification
import cn.jpush.api.push.model.notification.Notification
import com.alibaba.dubbo.config.annotation.Service
import com.bargetor.nest.common.springmvc.SpringApplicationUtil
import org.springframework.stereotype.Component
import javax.annotation.Resource

@Component
@Service(version = CubiloseService.version, async = false, retries = 0, timeout = 3000)
class CubiloseServiceImp: CubiloseService{
    @Resource
    private lateinit var jPushHelper: CubiloseJPushHelper

    override fun pushForAliases(aliases: List<String>, title: String, content: String, extra: Map<String, String>) {
        this.jPushHelper.push(this.buildPushObjectForAllAndAlias(aliases, title, content, extra))
    }

    //快速原型，这里不做优化
    fun buildPushObjectForAllAndAlias(aliases: List<String>, title: String, content: String, extra: Map<String, String>): PushPayload {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliases))
                .setNotification(
                        Notification.newBuilder()
                                .addPlatformNotification(
                                        IosNotification.newBuilder()
                                                .setAlert(IosAlert.newBuilder().setTitleAndBody(title, null, content).build())
                                                .addExtras(extra)
                                                .build()
                                )
                                .build()
                )
                .setMessage(
                        Message.newBuilder()
                                .setMsgContent(content)
                                .addExtras(extra)
                                .build()
                )
                .setOptions(Options.newBuilder().setApnsProduction(!SpringApplicationUtil.isDebugEnv()).build())
                .build()
    }
}